package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.ResultObj;
import com.luo.service.SlideShowService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能：
 * 作者：Luo。
 * 日期：2024/3/18 10:11
 */
@WebServlet("/updateSlideShow")
public class UpdateSlideShowController extends HttpServlet {
	SlideShowService slideShowService = new SlideShowService();

	@Override
	protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
		String shopId = req.getParameter("shopId");
		String slideshowId = req.getParameter("slideshowId");
		String type = req.getParameter("type");
		String imgUrl = req.getParameter("imgUrl");
		String hrefUrl = "/detail/" + shopId;
		type = "100".equals(type) ? "1" : "0";
		HashMap<String, String> map = new HashMap<>();
		map.put("shopId", shopId);
		map.put("slideshowId", slideshowId);
		map.put("type", type);
		map.put("imgUrl", imgUrl);
		map.put("hrefUrl", hrefUrl);
		ResultObj resultObj = new ResultObj();
		try {
			int i = slideShowService.updateSlideShow(map);
		} catch (Exception e) {
			e.printStackTrace();
			resultObj.setCode("500");
			resultObj.setMsg("未知错误");
			String jsonString = JSON.toJSONString(resultObj);
			resp.getWriter().write(jsonString);
			resp.getWriter().close();
			return;
		}
		resultObj.setCode("200");
		resultObj.setMsg("修改成功");
		String jsonString = JSON.toJSONString(resultObj);
		resp.getWriter().write(jsonString);
		resp.getWriter().close();
	}
}
